﻿using System.Runtime.CompilerServices;

namespace TestManage.Infrastructure.Extensions
{
    /// <summary>
    /// 提供对引擎的单件实例的访问。
    /// </summary>
    public class EngineContext
    {
        #region 方法
        /// <summary>
        /// Create a static instance of the Nop engine.
        /// </summary>
        [MethodImpl(MethodImplOptions.Synchronized)]
        public static IEngine Create()
        {
            //create NopEngine as engine
            return Singleton<IEngine>.Instance ?? (Singleton<IEngine>.Instance = new NopEngine());
        }

        /// <summary>
        /// 将静态引擎实例设置为提供的引擎。使用这个方法来提供您自己的引擎实现。
        /// </summary>
        public static void Replace(IEngine engine)
        {
            Singleton<IEngine>.Instance = engine;
        }

        #endregion

        #region 属性

        /// <summary>
        /// 获取单例Nop引擎，用于访问Nop服务。
        /// </summary>
        public static IEngine Current
        {
            get
            {
                if (Singleton<IEngine>.Instance == null)
                {
                    Create();
                }

                return Singleton<IEngine>.Instance;
            }
        }
        #endregion
    }
}
